function add(a,b,c) {
    return Array.from(arguments).reduce((total,value) => {
        return total + value;
    },0)
}

console.log(add(1,2,3));

// 柯里化

let curry = function(fun) {
    
    return function sum(...args) {
        if(args.length < fun.length) {
            return function() {
                return sum(...args,...arguments);
            }
        }else {
            return fun(...args);
        }
    }
    
} 

console.log(curry(add)(1,2)(2));

// Once
const Once = function(constructor) {
    let obj = null;
    return function(...args) {
        if(!obj) {
            obj = new constructor(...args);
        }
        return obj;
    }
}

function Person(name,age) {
    this.name = name;
    this.age = age;
}

let curriedPerson = Once(Person);

let daxia = curriedPerson('daxia',18);
let dada = curriedPerson('dada',99);

console.log(daxia);
console.log(dada);


// 柯里化接收无限个参数的函数
let curry1 = function(fun) {
    let sum = 0;
    function add1(...args) {
        sum += fun(...args);
        return add1;
    }

  	add1.valueOf = function() {
        return sum;
    }
    return add1;
}
function add1(...args) {
    return args.reduce((total,value) =>{
        return total+value;
    },0);
}

console.log(add(1,3,4));
console.log(curry1(add1)(1,2,3)(3)(4,5).valueOf())
